<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/8/18
 * Time: 16:37
 */

namespace webapi\modules\info\controllers;
use common\models\Product;
use webapi\controllers\BaseController;
use Yii;
use yii\data\ArrayDataProvider;
use yii\db\Query;

class DataController extends BaseController {

    /** 查询显示列 */
    public $selectParam = array(
        'product.id',
        'product.title',
        'product.pics',
        'product.is_brand',
        'product.banner_pic',
        'business.logo',
        'business.name',
        'business.province',
        'business.city',
        'business.area',
        'business.address',
        'business.type',
        'business.url',
        'business.location',
        'product.change_status',
        'product.description',
        'product.music',
        'business.phone',
        'product.businessId'
    );

    /**
     * 获取作品信息
     * @param pid 作品ID
     * @return array
     */
    public function actionView()
    {
        /** 作品pid */
        $pid = $_GET['pid'];
        if(!$pid){
            return $this->responseFail('pid is null.');
        }
        /** 组装where查询条件 */
        $where = array(
            'product.id' => $pid
        );
        /** 定义查询对象 */
        $query = new Query();
        $provider = new ArrayDataProvider([
            'allModels' => $query->select($this->selectParam)->from('product')
                ->innerJoin('business', 'product.businessId = business.id')
                ->where($where)->all(),
            'sort' => [
                'attributes' => ['product.id']
            ]
        ]);

        $returnData = array();
        /** 获取数据 */
        $models = $provider->getModels();
        if($models){
            $dn = Yii::$app->params['upload_path'];
            $models = $models[0];
            /** 处理图片 */
            $pdata = array();
            $picArray = explode(",", $models['pics']);
            foreach($picArray as $key => $val){
                if(empty($val)){
                    continue;
                }
                $path = $dn . substr($val, strripos($val, "images") + 7);
                if (file_exists($path)) {
                    list($width, $height, $type, $attr) = getimagesize($path);
                    $isV = 1;
                } else {
                    $isV = 0;
                    $width = 0;
                    $height = 0;
                }
                $pdata[] = array(
                    'isV' => $isV,
                    'width' => $width,
                    'height' => $height,
                    'pic' => $val
                );
            }

            $returnData = array(
                'serImg' => $pdata,
                'logoImg' => $models['logo'],
                'logoImg_n' => empty($models['logo']) ? '' : $this->createNewFile($models['logo']),
                'brandName' => $models['name'],
                'brandAddress' => $models['address'],
                'brand_type' => intval($models['type']) == 1 ? '影楼' : '工作室',
                'animateType' => $models['change_status'],
                'musical' => $models['music'],
                'tel' => $models['phone'],
                'businessId' => $models['businessId'],
                'title' => $models['title'],
                'province' => $models['province'],
                'city' => $models['city'],
                'url' => $models['url'],
                'area' => $models['area'],
                'location' => $models['location'],
                'description' => $models['description'],
                'is_brand' => $models['is_brand'],
                'banner_pic' => $models['banner_pic'],
                'banner_pic_n' => empty($models['banner_pic']) ? '' : $this->createNewFile($models['banner_pic'])
            );
        }

        /** 统计次数 */
        $product = Product::findIdentity($pid);
        if($product){
            $product->read_num = intval($product->read_num) + 1;
            $product->save();
        }

        /** 返回数据 */
        return $this->responseFormat($returnData);
    }

    private function createNewFile($img){
        return substr($img, 0, strrpos($img, '/'))  . '/N' . substr(strrchr($img, '/'), 1);
    }

}